<?php
namespace tools;

/**
 * 支付
 *
 */
use Org\Util\Date;
use think\facade\Log;
use think\facade\Env;
require_once Env::get('root_path') .'extend/tools/WeChatDeveloper/include.php';

class PayApi {
    /**
     * 微信支付
     * @return type 支付方式
     * @return goodsname 商品名称
     * @return order_no 订单编号
     * @return pay_money 支付金额
     * @return openid 用户openid
     * @return source 来源
     */
    public function preWxPay($type,$goodsname,$order_no,$pay_money='',$openid='',$source='ios') {
        $config =config('wechat.wechat_xiaochengxu');
        // 创建接口实例
        $wechat = new \WePayV3\Order($config);

        //微信小程序/微信公众号
        if($type=='jsapi'){
            $options = [
                'description'      => $goodsname,
                'out_trade_no'     => $order_no,
                'amount'           =>[
                      "total" =>(int)(($pay_money*1000)/10),
                      "currency" => "CNY", 
                ],
                "payer"=>[
                  "openid"=>$openid
                ],
                'notify_url'       => $config['notify_url'],
                'appid'            => $config['appid'],
                'mchid'            => $config['mch_id'],
            ];
        }

        //微信H5
        if($type=='h5'){
            $options = [
                'description'      => $goodsname,
                'out_trade_no'     => $order_no,
                'amount'           =>[
                      "total" =>(int)(($pay_money*1000)/10),
                      "currency" => "CNY", 
                ],
                'notify_url'       => $config['notify_url'],
                'appid'            => $config['appid'],
                'mchid'            => $config['mch_id'],
                'scene_info'       =>[
                    'payer_client_ip'  =>request()->ip(),
                    'h5_info'=>[
                        'type'=>$source
                    ]
                ]
            ];
        }

        //微信app
        if($type=='app'){
            $options = [
                'description'      => $goodsname,
                'out_trade_no'     => $order_no,
                'amount'           =>[
                      "total" =>(int)(($pay_money*1000)/10),
                      "currency" => "CNY", 
                ],
                'notify_url'       => $config['notify_url'],
                'appid'            => $config['appid'],
                'mchid'            => $config['mch_id']
            ];
        }

        //微信native(pc二维码)
        if($type=='native'){
            $options = [
                'description'      => $goodsname,
                'out_trade_no'     => $order_no,
                'amount'           =>[
                      "total" =>(int)(($pay_money*1000)/10),
                      "currency" => "CNY", 
                ],
                'notify_url'       => $config['notify_url'],
                'appid'            => $config['appid'],
                'mchid'            => $config['mch_id']
            ];
        }
        $wxData =$wechat->create($type,$options);
        return $wxData;
    }


     /**
     * 微信退款
     * @return type 支付方式
     * @return goodsname 商品名称
     * @return order_no 订单编号
     * @return pay_money 支付金额
     * @return openid 用户openid
     * @return source 来源
     */
    public function preWxRefund($transaction_id,$out_refund_no,$totalFee,$refundFee) {
        $config =config('wechat.wechat_xiaochengxu');
        // 创建接口实例
        $wechat = new \WePayV3\Refund($config);
        $options = [
            'transaction_id' => $transaction_id,
            'out_refund_no'  => $out_refund_no,
            'amount'           =>[
                "refund" =>(int)(($refundFee*1000)/10),
                "total"=>(int)(($totalFee*1000)/10),
                "currency"=>"CNY",
            ]
        ];
        $wxData =$wechat->create($options);
        return $wxData;
    }
}